//
//  ViewController.m
//  王宇先_03_计算器2
//
//  Created by wyx on 16/3/12.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ViewController ()

@property(retain,nonatomic) UIButton *button_number;
@property(retain,nonatomic) UILabel *label_display;
@property(retain,nonatomic) NSMutableString *muStr;//用来拼接输入的数字.
@property(assign,nonatomic) double num1,num2;
@property(assign,nonatomic) NSString *str;//用来保存符号.

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createCalculator];
}
- (void)createCalculator{
    
    //1,设置键盘背景色(数字部分保留了这颜色).
    self.view.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    
    //2,创建显示实时输入label_display.
    self.label_display=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - WIDTH / 4 * 5)];
    [self.view addSubview:_label_display];
    self.label_display.backgroundColor=[UIColor colorWithWhite:0.098 alpha:1.000];//设置背景颜色
    self.label_display.textColor=[UIColor whiteColor];//字体颜色
    self.label_display.textAlignment = NSTextAlignmentRight;//字体居右
    self.label_display.font=[UIFont systemFontOfSize:60];//设置字体大小.
    [_label_display release];
    
    //3,循环添加数字1-9按钮.
    NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    
    int index = 0;//充当数组下标.
    
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            self.button_number=[UIButton buttonWithType:UIButtonTypeCustom];
            self.button_number.frame=CGRectMake(WIDTH / 4 * j, HEIGHT - WIDTH + WIDTH / 4 * i, WIDTH / 4, WIDTH /4);
            self.button_number.layer.borderWidth = 1;//设置边框宽.
            [self.button_number setTitle:[array objectAtIndex:index++] forState:UIControlStateNormal];//根据下标依次给button赋标题.
            [self.button_number setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//修改标题字体颜色.
            self.button_number.titleLabel.font = [UIFont systemFontOfSize:40];//修改标题字体大小.
            //注意：[array objectAtIndex:index++]
            [self.view addSubview:_button_number];
            [self.button_number addTarget:self action:@selector(displayNumber:) forControlEvents:UIControlEventTouchUpInside];
            //addTarget:self 的意思是说，这个方法在本类中也可以传入其他类的指针.
        }
    }
    
    
    //4,单独添加按钮0.
    UIButton *button_zero=[UIButton buttonWithType:UIButtonTypeCustom];
    [button_zero setFrame:CGRectMake(0, HEIGHT - WIDTH / 4, WIDTH / 4 * 2, WIDTH / 4)];//设置button在view上的位置
    button_zero.layer.borderWidth = 1;
    [button_zero setTitle:@"0" forState:UIControlStateNormal];
    [button_zero setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button_zero.titleLabel.font = [UIFont systemFontOfSize:40];
    //按下按钮，并且当手指离开离开屏幕的时候触发这个事件;触发了这个事件后，执行displayNumber:方法，action:@selector(displayNumber:).
    [button_zero addTarget:self action:@selector(displayNumber:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_zero];//显示控件
    
    
    //5,循环添加运算符按钮.
    NSArray *array_operator=[NSArray arrayWithObjects:@"+",@"-",@"*",@"/",nil];
    for (int i=0; i<4; i++)
    {
        UIButton *button_operator=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button_operator setFrame:CGRectMake(WIDTH / 4 * 3, HEIGHT - WIDTH / 4 * 5 + WIDTH / 4 * i, WIDTH / 4, WIDTH /4)];
        button_operator.layer.borderWidth = 1;
        [button_operator setTitle:[array_operator objectAtIndex:i] forState:UIControlStateNormal];
        [button_operator setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button_operator.titleLabel.font = [UIFont systemFontOfSize:40];
        button_operator.backgroundColor = [UIColor colorWithRed:0.953 green:0.498 blue:0.149 alpha:1.000];
        [self.view addSubview:button_operator];
        [button_operator addTarget:self action:@selector(calculate:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //6,添加 =.
    UIButton *button_equal=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button_equal setFrame:CGRectMake(WIDTH / 4 * 3, HEIGHT - WIDTH / 4, WIDTH / 4, WIDTH / 4)];
    button_equal.layer.borderWidth = 1;
    [button_equal setTitle:@"=" forState:UIControlStateNormal];
    [button_equal setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button_equal.titleLabel.font = [UIFont systemFontOfSize:40];
    button_equal.backgroundColor = [UIColor colorWithRed:0.953 green:0.498 blue:0.149 alpha:1.000];
    [button_equal addTarget:self action:@selector(calculate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_equal];
    
    
    //7,添加清除键AC.
    UIButton *button_clean=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button_clean setFrame:CGRectMake(0, HEIGHT - WIDTH / 4 * 5, WIDTH / 4, WIDTH / 4)];
    button_clean.layer.borderWidth = 1;
    [button_clean setTitle:@"AC" forState:UIControlStateNormal];
    [button_clean setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button_clean.titleLabel.font = [UIFont systemFontOfSize:40];
    button_clean.backgroundColor = [UIColor colorWithWhite:0.804 alpha:1.000];
    [button_clean addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_clean];
    
    
    //8,添加小数点按钮.
    UIButton *button_point=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button_point setFrame:CGRectMake(WIDTH / 4 * 2, HEIGHT - WIDTH / 4, WIDTH / 4, WIDTH / 4)];
    button_point.layer.borderWidth = 1;
    [button_point setTitle:@"." forState:UIControlStateNormal];
    [button_point setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button_point.titleLabel.font = [UIFont systemFontOfSize:40];
    [button_point addTarget:self action:@selector(displayNumber:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_point];
    
    
    //9,后退按钮(用+/-,%以假乱真)
    UIButton *button_backLeft=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button_backLeft setFrame:CGRectMake(WIDTH / 4, HEIGHT - WIDTH / 4 * 5, WIDTH / 4, WIDTH / 4)];
    button_backLeft.layer.borderWidth = 1;
    [button_backLeft setTitle:@"+/-" forState:UIControlStateNormal];
    [button_backLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button_backLeft.titleLabel.font = [UIFont systemFontOfSize:40];
    button_backLeft.backgroundColor = [UIColor colorWithWhite:0.804 alpha:1.000];
    [button_backLeft addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_backLeft];
    
    UIButton *button_backRight =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button_backRight setFrame:CGRectMake(WIDTH / 4 * 2, HEIGHT - WIDTH / 4 * 5, WIDTH / 4, WIDTH / 4)];
    button_backRight.layer.borderWidth = 1;
    [button_backRight setTitle:@"%" forState:UIControlStateNormal];
    [button_backRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button_backRight.titleLabel.font = [UIFont systemFontOfSize:40];
    button_backRight.backgroundColor = [UIColor colorWithWhite:0.804 alpha:1.000];
    [button_backRight addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_backRight];
    
    //10,初始化字符串,分配内存.
    self.muStr=[[NSMutableString alloc]init];
    self.str = [[NSString alloc]init];
    
}

/** 数字按钮点击方法. */
- (void)displayNumber:(id)sender{
    
    [self.muStr appendString:[sender currentTitle]];//拼接字符串实现数字连续输入,currentTitle是button上显示的标题.
    self.label_display.text=[NSString stringWithString:_muStr];//把字符串设置成label_display文本,实现实时显示数值.
    self.num1 = [self.label_display.text doubleValue];//输入的数值文本为对象类型转成属性类型保存到属性num1里.
    //    NSLog(@"self.num1  is  %g",self.num1);
    
}
//计算方法
-(void)calculate:(id)sender{
    
    if ([self.str isEqualToString:@""])//当str里为空
    {
        self.num2 = self.num1;
        //        NSLog(@"self.num2  is  %g",self.num2);
        self.label_display.text = [NSString stringWithString:_muStr];//只要是符号就显示数值
        [self.muStr setString:@""];//字符串清零
        self.str = [sender currentTitle];//保存运算符为了作判断作何种运算
        //        NSLog(@"%@",_str);
        [self.muStr appendString:self.str];
        self.label_display.text = [NSString stringWithString:_muStr];//显示数值
        [self.muStr setString:@""];//字符串清零
    }
    else
    {
        //输出上次计算结果
        if ([self.str isEqualToString:@"+"])//之前的符号是+
        {
            [self.muStr setString:@""];//字符串清零
            self.num2 += self.num1;//num2是运算符号左边的数值，还是计算结果.
            
            //输出上次结果后判断这次输入的是何符号.
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                //                NSLog(@"self.num2 is %g",self.num2);
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                self.str = @"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                //                NSLog(@"self.num2 is %g",self.num2);
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                [self.muStr setString:@""];   //字符串清零
                self.str = [sender currentTitle];//保存运算符为了作判断作何种运算
                //                NSLog(@"%@",_str);
                [self.muStr appendString:self.str];
                [self.muStr setString:@""];//字符串清零
            }
        }
        
        else if ([self.str isEqualToString:@"-"])//之前的符号是-
        {
            [self.muStr setString:@""];//字符串清零
            self.num2 -= self.num1;
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                //                NSLog(@"self.num2  is  %g",self.num2);
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                self.str = @"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                //                NSLog(@"self.num2  is  %g",self.num2);
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                [self.muStr setString:@""];//字符串清零
                self.str = [sender currentTitle];//保存运算符为了作判断作何种运算
                //                NSLog(@"%@",_str);
                [self.muStr appendString:self.str];
                [self.muStr setString:@""];//字符串清零
            }
        }
        
        else if([self.str hasPrefix:@"*"])//之前的符号是*   hasPrefix:方法的功能是判断创建的字符串内容是否以某个字符开始
        {
            [self.muStr setString:@""];//字符串清零
            self.num2 *= self.num1;
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle] isEqualToString:@"="])
            {
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                self.str = @"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                [self.muStr setString:@""];          //字符串清零
                self.str = [sender currentTitle];       //保存运算符为了作判断作何种运算
                [self.muStr appendString:self.str];  //在字符串后增加新的东西，［a appendString:]
                [self.muStr setString:@""];          //字符串清零
            }
        }
        
        else if ([self.str isEqualToString:@"/"])//之前的符号是/
        {
            [self.muStr setString:@""];//字符串清零
            self.num2 /= self.num1;
            //判断输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                //                NSLog(@"self.num2  is  %g",self.num2);
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                self.str = @"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                //                NSLog(@"self.num2  is  %g",self.num2);
                self.label_display.text = [NSString stringWithFormat:@"%g",self.num2];
                [self.muStr setString:@""];//字符串清零
                self.str = [sender currentTitle];//保存运算符为了作判断作何种运算
                //                NSLog(@"%@",_str);
                [self.muStr appendString:self.str];
                [self.muStr setString:@""];//字符串清零
            }
        }
    }
}


//当按下清除建时，所有数据清零
-(void)clean:(id)sender{
    
    [self.muStr setString:@""];//清空字符
    self.num1 = 0;
    self.num2 = 0;
    self.label_display.text = @"0";//保证下次输入时清零
}

//返回键
-(void)back:(id)sender{
    
    if (self.label_display.text != nil){//判断不是空
        
        [self.muStr deleteCharactersInRange:NSMakeRange([self.muStr length]-2,1)];//删除最后一个字符
        self.label_display.text = [NSString stringWithString:_muStr];//显示结果
    }
    else{
        
        NSLog(@"什么都没输入呢!");
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
